﻿using FarmSmartSleep.Common.Domain.Sales_Purchase;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace FarmSmartSleep.Organ.API.Application.Sales_Purchase.PersonalProcurement
{
    /// <summary>
    /// 采购列表
    /// </summary>
    public class PersonalProcurementListHandler:IRequestHandler<PersonalProcurementListCommon, ResultPage<List<Proc>>>
    {
        private readonly IBaseRepository<Proc> procRep;

        /// <summary>
        /// 采购列表
        /// </summary>
        public PersonalProcurementListHandler(IBaseRepository<Proc> procRep)
        {
            this.procRep = procRep;
        }

        /// <summary>
        /// 采购列表
        /// </summary>
        public async Task<ResultPage<List<Proc>>> Handle(PersonalProcurementListCommon request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<List<Proc>>() { Code = (int)CodeEnum.search};
            try
            {
                var listQuery = procRep.GetAll();


                if(request.PurchaseDate!=null)
                {
                    listQuery = listQuery.Where(c => c.PurchaseDate == request.PurchaseDate);
                }
                if(!string.IsNullOrEmpty(request.Category))
                {
                    listQuery = listQuery.Where(c => c.Category == request.Category);
                }
                if (!string.IsNullOrEmpty(request.Warehouse))
                {
                    listQuery = listQuery.Where(c => c.Warehouse == request.Warehouse);
                }

                if (request.TransferBuild !=0)
                {
                    listQuery = listQuery.Where(c => c.TransferBuild == request.TransferBuild);
                } 
                if (request.EarId != 0)
                {
                    listQuery = listQuery.Where(c => c.EarId == request.EarId);
                }
                if (request.Buyer != 0)
                {
                    listQuery = listQuery.Where(c => c.Buyer == request.Buyer);
                }
                if (!string.IsNullOrEmpty(request.DocumentNumber))
                {
                    listQuery = listQuery.Where(c => c.DocumentNumber == request.DocumentNumber);
                }
                if (request.Supplier != 0)
                {
                    listQuery = listQuery.Where(c => c.Supplier == request.Supplier);
                }
                //仅在筛选后转换为列表
                var list = listQuery.ToList();
                result.totalCount = list.Count();
                result.totalPage = (int)Math.Ceiling(result.totalCount / (double)request.PageSize);
                list = list.OrderBy(c => c.ProcId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                if(list.Count()==0)
                {
                    result.Messege = "暂无数据";
                }

                result.Code = (int)CodeEnum.success;
                result.Messege = "查询成功";
                result.Item = list.ToList();

            }
            catch (Exception)
            {
                result.Messege = "查询异常";
                throw;
            }
            return result;
        }
    }
}
